home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / IFF / iff.h < prev    next >
Text File  |  1989-05-30  |  20KB  |  437 lines

  1. #ifndef IFF_H
  2. #define IFF_H
  3. /*----------------------------------------------------------------------*/
  4. /* IFF.H  defs for IFF-85 Interchange Format Files.           11/15/85 */
  5. /*                                    */
  6. /* By Jerry Morrison and Steve Shaw, Electronic Arts.            */
  7. /* This software is in the public domain.                */
  8. /*----------------------------------------------------------------------*/
  9.  
  10. #ifndef EXEC_TYPES_H
  11. #include "exec/types.h"
  12. #endif
  13.  
  14. #ifndef LIBRARIES_DOS_H
  15. #include "libraries/dos.h"
  16. #endif
  17.  
  18. typedef LONG IFFP;    /* Status code result from an IFF procedure */
  19.     /* LONG, because must be type compatable with ID for GetChunkHdr.*/
  20.     /* Note that the error codes below are not legal IDs.*/
  21. #define IFF_OKAY  0    /* Keep going...*/
  22. #define END_MARK  -1    /* As if there was a chunk at end of group.*/
  23. #define IFF_DONE  -2    /* clientProc returns this when it has READ enough.
  24.              * It means return thru all levels. File is Okay.*/
  25. #define DOS_ERROR -3
  26. #define NOT_IFF   -4    /* not an IFF file.*/
  27. #define NO_FILE   -5    /* Tried to open file, DOS didn't find it.*/
  28. #define CLIENT_ERROR -6    /* Client made invalid request, for instance, write a
  29.              * negative size chunk.*/
  30. #define BAD_FORM  -7    /* A client read proc complains about FORM semantics;
  31.              * e.g. valid IFF, but missing a required chunk.*/
  32. #define SHORT_CHUNK -8    /* Client asked to IFFReadBytes more bytes than left
  33.              * in the chunk. Could be client bug or bad form.*/
  34. #define BAD_IFF   -9    /* mal-formed IFF file. [TBD] Expand this into a
  35.              * range of error codes.*/
  36. #define LAST_ERROR BAD_IFF
  37.  
  38. /* This MACRO is used to RETURN immediately when a termination condition is
  39.  * found. This is a pretty weird macro. It requires the caller to declare a
  40.  * local "IFFP iffp" and assign it. This wouldn't work as a subroutine since
  41.  * it returns for it's caller. */
  42. #define CheckIFFP()   { if (iffp != IFF_OKAY) return(iffp); }
  43.  
  44.  
  45. /* ---------- ID -------------------------------------------------------*/
  46.  
  47. typedef LONG ID;    /* An ID is four printable ASCII chars but
  48.              * stored as a LONG for efficient copy & compare.*/
  49.  
  50. /* Four-character IDentifier builder.*/
  51. #define MakeID(a,b,c,d)  ( (a)<<24 | (b)<<16 | (c)<<8 | (d) )
  52.  
  53. /* Standard group IDs.  A chunk with one of these IDs contains a
  54.    SubTypeID followed by zero or more chunks.*/
  55. #define FORM MakeID('F','O','R','M')
  56. #define PROP MakeID('P','R','O','P')
  57. #define LIST MakeID('L','I','S','T')
  58. #define CAT  MakeID('C','A','T',' ')
  59. #define FILLER MakeID(' ',' ',' ',' ')
  60. /* The IDs "FOR1".."FOR9", "LIS1".."LIS9", & "CAT1".."CAT9" are reserved
  61.  * for future standardization.*/
  62.  
  63. /* Pseudo-ID used internally by chunk reader and writer.*/
  64. #define NULL_CHUNK 0L           /* No current chunk.*/
  65.  
  66.  
  67. /* ---------- Chunk ----------------------------------------------------*/
  68.  
  69. /* All chunks start with a type ID and a count of the data bytes that 
  70.    follow--the chunk's "logical size" or "data size". If that number is odd,
  71.    a 0 pad byte is written, too. */
  72. typedef struct {
  73.     ID      ckID;
  74.     LONG  ckSize;
  75.     } ChunkHeader;
  76.  
  77. typedef struct {
  78.     ID      ckID;
  79.     LONG  ckSize;
  80.     UBYTE ckData[ 1 /*REALLY: ckSize*/ ];
  81.     } Chunk;
  82.  
  83. /* Pass ckSize = szNotYetKnown to the writer to mean "compute the size".*/
  84. #define szNotYetKnown 0x80000001L
  85.  
  86. /* Need to know whether a value is odd so can word-align.*/
  87. #define IS_ODD(a)   ((a) & 1)
  88.  
  89. /* This macro rounds up to an even number. */
  90. #define WordAlign(size)   ((size+1)&~1)
  91.  
  92. /* ALL CHUNKS MUST BE PADDED TO EVEN NUMBER OF BYTES.
  93.  * ChunkPSize computes the total "physical size" of a padded chunk from
  94.  * its "data size" or "logical size". */
  95. #define ChunkPSize(dataSize)  (WordAlign(dataSize) + sizeof(ChunkHeader))
  96.  
  97. /* The Grouping chunks (LIST, FORM, PROP, & CAT) contain concatenations of
  98.  * chunks after a subtype ID that identifies the content chunks.
  99.  * "FORM type XXXX", "LIST of FORM type XXXX", "PROPerties associated
  100.  * with FORM type XXXX", or "conCATenation of XXXX".*/
  101. typedef struct {
  102.     ID      ckID;
  103.     LONG  ckSize;    /* this ckSize includes "grpSubID".*/
  104.     ID    grpSubID;
  105.     } GroupHeader;
  106.  
  107. typedef struct {
  108.     ID      ckID;
  109.     LONG  ckSize;
  110.     ID    grpSubID;
  111.     UBYTE grpData[ 1 /*REALLY: ckSize-sizeof(grpSubID)*/ ];
  112.     } GroupChunk;
  113.  
  114.  
  115. /* ---------- IFF Reader -----------------------------------------------*/
  116.  
  117. /******** Routines to support a stream-oriented IFF file reader *******
  118.  *
  119.  * These routines handle lots of details like error checking and skipping
  120.  * over padding. They're also careful not to read past any containing context.
  121.  *
  122.  * These routines ASSUME they're the only ones reading from the file.
  123.  * Client should check IFFP error codes. Don't press on after an error!
  124.  * These routines try to have no side effects in the error case, except
  125.  * partial I/O is sometimes unavoidable.
  126.  *
  127.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  128.  * specific error code.
  129.  *
  130.  * The overall scheme for the low level chunk reader is to open a "group read
  131.  * context" with OpenRIFF or OpenRGroup, read the chunks with GetChunkHdr
  132.  * (and its kin) and IFFReadBytes, and close the context with CloseRGroup.
  133.  *
  134.  * The overall scheme for reading an IFF file is to use ReadIFF, ReadIList,
  135.  * and ReadICat to scan the file. See those procedures, ClientProc (below),
  136.  * and the skeleton IFF reader. */
  137.  
  138. /* Client passes ptrs to procedures of this type to ReadIFF which call them
  139.  * back to handle LISTs, FORMs, CATs, and PROPs.
  140.  *
  141.  * Use the GroupContext ptr when calling reader routines like GetChunkHdr.
  142.  * Look inside the GroupContext ptr for your ClientFrame ptr. You'll
  143.  * want to type cast it into a ptr to your containing struct to get your
  144.  * private contextual data (stacked property settings). See below. */
  145. typedef IFFP ClientProc(struct _GroupContext *);
  146.  
  147. /* Client's context for reading an IFF file or a group.
  148.  * Client should actually make this the first component of a larger struct
  149.  * (it's personal stack "frame") that has a field to store each "interesting"
  150.  * property encountered.
  151.  * Either initialize each such field to a global default or keep a boolean
  152.  * indicating if you've read a property chunk into that field.
  153.  * Your getList and getForm procs should allocate a new "frame" and copy the
  154.  * parent frame's contents. The getProp procedure should store into the frame
  155.  * allocated by getList for the containing LIST. */
  156. typedef struct _ClientFrame {
  157.     ClientProc *getList, *getProp, *getForm, *getCat;
  158.     /* client's own data follows; place to stack property settings */
  159.     } ClientFrame;
  160.  
  161. /* Our context for reading a group chunk. */
  162. typedef struct _GroupContext {
  163.     struct _GroupContext *parent; /* Containing group; NULL => whole file. */
  164.     ClientFrame *clientFrame;     /* Reader data & client's context state. */
  165.     BPTR file;        /* Byte-stream file handle. */
  166.     LONG position;    /* The context's logical file position. */
  167.     LONG bound;        /* File-absolute context bound
  168.              * or szNotYetKnown (writer only). */
  169.     ChunkHeader ckHdr;    /* Current chunk header. ckHdr.ckSize = szNotYetKnown
  170.              * means we need to go back and set the size (writer only).
  171.              * See also Pseudo-IDs, above. */
  172.     ID subtype;        /* Group's subtype ID when reading. */
  173.     LONG bytesSoFar;    /* # bytes read/written of current chunk's data. */
  174.     } GroupContext;
  175.  
  176. /* Computes the number of bytes not yet read from the current chunk, given
  177.  * a group read context gc. */
  178. #define ChunkMoreBytes(gc)  ((gc)->ckHdr.ckSize - (gc)->bytesSoFar)
  179.  
  180.  
  181. /***** Low Level IFF Chunk Reader *****/
  182.  
  183. /* Given an open file, open a read context spanning the whole file.
  184.  * This is normally only called by ReadIFF.
  185.  * This sets new->clientFrame = clientFrame.
  186.  * ASSUME context allocated by caller but not initialized.
  187.  * ASSUME caller doesn't deallocate the context before calling CloseRGroup.
  188.  * NOT_IFF ERROR if the file is too short for even a chunk header.*/
  189. extern IFFP OpenRIFF(BPTR, GroupContext *, ClientFrame *);
  190.              /*  file, new,            clientFrame  */
  191.  
  192. /* Open the remainder of the current chunk as a group read context.
  193.  * This will be called just after the group's subtype ID has been read
  194.  * (automatically by GetChunkHdr for LIST, FORM, PROP, and CAT) so the
  195.  * remainder is a sequence of chunks.
  196.  * This sets new->clientFrame = parent->clientFrame. The caller should repoint
  197.  * it at a new clientFrame if opening a LIST context so it'll have a "stack
  198.  * frame" to store PROPs for the LIST. (It's usually convenient to also
  199.  * allocate a new Frame when you encounter FORM of the right type.)
  200.  *
  201.  * ASSUME new context allocated by caller but not initialized.
  202.  * ASSUME caller doesn't deallocate the context or access the parent context
  203.  * before calling CloseRGroup.
  204.  * BAD_IFF ERROR if context end is odd or extends past parent. */
  205. extern IFFP OpenRGroup(GroupContext *, GroupContext *);
  206.            /*  parent,         new  */
  207.  
  208. /* Close a group read context, updating its parent context.
  209.  * After calling this, the old context may be deallocated and the parent
  210.  * context can be accessed again. It's okay to call this particular procedure
  211.  * after an error has occurred reading the group.
  212.  * This always returns IFF_OKAY. */
  213. extern IFFP CloseRGroup(GroupContext *);
  214.             /*  old  */
  215.  
  216. /* Skip any remaining bytes of the previous chunk and any padding, then
  217.  * read the next chunk header into context.ckHdr.
  218.  * If the ckID is LIST, FORM, CAT, or PROP, this automatically reads the
  219.  * subtype ID into context->subtype.
  220.  * Caller should dispatch on ckID (and subtype) to an appropriate handler.
  221.  *
  222.  * RETURNS context.ckHdr.ckID (the ID of the new chunk header); END_MARK
  223.  * if there are no more chunks in this context; or NOT_IFF if the top level
  224.  * file chunk isn't a FORM, LIST, or CAT; or BAD_IFF if malformed chunk, e.g.
  225.  * ckSize is negative or too big for containing context, ckID isn't positive,
  226.  * or we hit end-of-file.
  227.  *
  228.  * See also GetFChunkHdr, GetF1ChunkHdr, and GetPChunkHdr, below.*/
  229. extern ID       GetChunkHdr(GroupContext *);
  230.   /*  context.ckHdr.ckID    context  */
  231.  
  232. /* Read nBytes number of data bytes of current chunk. (Use OpenGroup, etc.
  233.  * instead to read the contents of a group chunk.) You can call this several
  234.  * times to read the data piecemeal.
  235.  * CLIENT_ERROR if nBytes < 0. SHORT_CHUNK if nBytes > ChunkMoreBytes(context)
  236.  * which could be due to a client bug or a chunk that's shorter than it
  237.  * ought to be (bad form). (on either CLIENT_ERROR or SHORT_CHUNK,
  238.  * IFFReadBytes won't read any bytes.) */
  239. extern IFFP IFFReadBytes(GroupContext *, BYTE *, LONG);
  240.              /*  context,        buffer, nBytes  */
  241.  
  242.  
  243. /***** IFF File Reader *****/
  244.  
  245. /* This is a noop ClientProc that you can use for a getList, getForm, getProp,
  246.  * or getCat procedure that just skips the group. A simple reader might just
  247.  * implement getForm, store &ReadICat in the getCat field of clientFrame, and
  248.  * use &SkipGroup for the getList and getProp procs.*/
  249. extern IFFP SkipGroup(GroupContext *);
  250.  
  251. /* IFF file reader.
  252.  * Given an open file, allocate a group context and use it to read the FORM,
  253.  * LIST, or CAT and it's contents. The idea is to parse the file's contents,
  254.  * and for each FORM, LIST, CAT, or PROP encountered, call the getForm,
  255.  * getList, getCat, or getProp procedure in clientFrame, passing the
  256.  * GroupContext ptr.
  257.  * This is achieved with the aid of ReadIList (which your getList should
  258.  * call) and ReadICat (which your getCat should call, if you don't just use
  259.  * &ReadICat for your getCat). If you want to handle FORMs, LISTs, and CATs
  260.  * nested within FORMs, the getForm procedure must dispatch to getForm,
  261.  * getList, and getCat (it can use GetF1ChunkHdr to make this easy).
  262.  *
  263.  * Normal return is IFF_OKAY (if whole file scanned) or IFF_DONE (if a client
  264.  * proc said "done" first).
  265.  * See the skeletal getList, getForm, getCat, and getProp procedures. */
  266. extern IFFP ReadIFF(BPTR, ClientFrame *);
  267.                 /*  file, clientFrame  */
  268.  
  269. /* IFF LIST reader.
  270.  * Your "getList" procedure should allocate a ClientFrame, copy the parent's
  271.  * ClientFrame, and then call this procedure to do all the work.
  272.  *
  273.  * Normal return is IFF_OKAY (if whole LIST scanned) or IFF_DONE (if a client
  274.  * proc said "done" first).
  275.  * BAD_IFF ERROR if a PROP appears after a non-PROP. */
  276. extern IFFP ReadIList(GroupContext *, ClientFrame *);
  277.           /*  parent,         clientFrame  */
  278.  
  279. /* IFF CAT reader.
  280.  * Most clients can simply use this to read their CATs. If you must do extra
  281.  * setup work, put a ptr to your getCat procedure in the clientFrame, and
  282.  * have that procedure call ReadICat to do the detail work.
  283.  *
  284.  * Normal return is IFF_OKAY (if whole CAT scanned) or IFF_DONE (if a client
  285.  * proc said "done" first).
  286.  * BAD_IFF ERROR if a PROP appears in the CAT. */
  287. extern IFFP ReadICat(GroupContext *);
  288.          /*  parent  */
  289.  
  290. /* Call GetFChunkHdr instead of GetChunkHdr to read each chunk inside a FORM.
  291.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a PROP chunk. */
  292. extern ID    GetFChunkHdr(GroupContext *);
  293.   /*  context.ckHdr.ckID    context  */
  294.  
  295. /* GetF1ChunkHdr is like GetFChunkHdr, but it automatically dispatches to the
  296.  * getForm, getList, and getCat procedure (and returns the result) if it
  297.  * encounters a FORM, LIST, or CAT. */
  298. extern ID    GetF1ChunkHdr(GroupContext *);
  299.   /*  context.ckHdr.ckID    context  */
  300.  
  301. /* Call GetPChunkHdr instead of GetChunkHdr to read each chunk inside a PROP.
  302.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a group chunk. */
  303. extern ID    GetPChunkHdr(GroupContext *);
  304.   /*  context.ckHdr.ckID    context  */
  305.  
  306.  
  307. /* ---------- IFF Writer -----------------------------------------------*/
  308.  
  309. /******* Routines to support a stream-oriented IFF file writer *******
  310.  *
  311.  * These routines will random access back to set a chunk size value when the
  312.  * caller doesn't know it ahead of time. They'll also do things automatically
  313.  * like padding and error checking.
  314.  *
  315.  * These routines ASSUME they're the only ones writing to the file.
  316.  * Client should check IFFP error codes. Don't press on after an error!
  317.  * These routines try to have no side effects in the error case, except that
  318.  * partial I/O is sometimes unavoidable.
  319.  *
  320.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  321.  * specific error code.
  322.  *
  323.  * The overall scheme is to open an output GroupContext via OpenWIFF or
  324.  * OpenWGroup, call either PutCk or {PutCkHdr {IFFWriteBytes}* PutCkEnd} for
  325.  * each chunk, then use CloseWGroup to close the GroupContext.
  326.  *
  327.  * To write a group (LIST, FORM, PROP, or CAT), call StartWGroup, write out
  328.  * its chunks, then call EndWGroup. StartWGroup automatically writes the
  329.  * group header and opens a nested context for writing the contents.
  330.  * EndWGroup closes the nested context and completes the group chunk. */
  331.  
  332.  
  333. /* Given a file open for output, open a write context.
  334.  * The "limit" arg imposes a fence or upper limit on the logical file
  335.  * position for writing data in this context. Pass in szNotYetKnown to be
  336.  * bounded only by disk capacity.
  337.  * ASSUME new context structure allocated by caller but not initialized.
  338.  * ASSUME caller doesn't deallocate the context before calling CloseWGroup.
  339.  * The caller is only allowed to write out one FORM, LIST, or CAT in this top
  340.  * level context (see StartWGroup and PutCkHdr).
  341.  * CLIENT_ERROR if limit is odd.*/
  342. extern IFFP OpenWIFF(BPTR, GroupContext *, LONG);
  343.          /*  file, new,            limit {file position}  */
  344.  
  345. /* Start writing a group (presumably LIST, FORM, PROP, or CAT), opening a
  346.  * nested context. The groupSize includes all nested chunks + the subtype ID.
  347.  *
  348.  * The subtype of a LIST or CAT is a hint at the contents' FORM type(s). Pass
  349.  * in FILLER if it's a mixture of different kinds.
  350.  *
  351.  * This writes the chunk header via PutCkHdr, writes the subtype ID via
  352.  * IFFWriteBytes, and calls OpenWGroup. The caller may then write the nested
  353.  * chunks and finish by calling EndWGroup.
  354.  * The OpenWGroup call sets new->clientFrame = parent->clientFrame.
  355.  *
  356.  * ASSUME new context structure allocated by caller but not initialized.
  357.  * ASSUME caller doesn't deallocate the context or access the parent context
  358.  * before calling CloseWGroup.
  359.  * ERROR conditions: See PutCkHdr, IFFWriteBytes, OpenWGroup. */
  360. extern IFFP StartWGroup(GroupContext *, ID, LONG, ID, GroupContext *);
  361.             /*  parent, groupType, groupSize, subtype, new  */
  362.  
  363. /* End a group started by StartWGroup.
  364.  * This just calls CloseWGroup and PutCkEnd.
  365.  * ERROR conditions: See CloseWGroup and PutCkEnd. */
  366. extern IFFP EndWGroup(GroupContext *);
  367.             /*  old  */
  368.  
  369. /* Open the remainder of the current chunk as a group write context.
  370.  * This is normally only called by StartWGroup.
  371.  *
  372.  * Any fixed limit to this group chunk or a containing context will impose
  373.  * a limit on the new context.
  374.  * This will be called just after the group's subtype ID has been written
  375.  * so the remaining contents will be a sequence of chunks.
  376.  * This sets new->clientFrame = parent->clientFrame.
  377.  * ASSUME new context structure allocated by caller but not initialized.
  378.  * ASSUME caller doesn't deallocate the context or access the parent context
  379.  * before calling CloseWGroup.
  380.  * CLIENT_ERROR if context end is odd or PutCkHdr wasn't called first. */
  381. extern IFFP OpenWGroup(GroupContext *, GroupContext *);
  382.            /*  parent,         new  */
  383.  
  384. /* Close a write context and update its parent context.
  385.  * This is normally only called by EndWGroup.
  386.  *
  387.  * If this is a top level context (created by OpenWIFF) we'll set the file's
  388.  * EOF (end of file) but won't close the file.
  389.  * After calling this, the old context may be deallocated and the parent
  390.  * context can be accessed again.
  391.  *
  392.  * Amiga DOS Note: There's no call to set the EOF. We just position to the
  393.  * desired end and return. Caller must Close file at that position.
  394.  * CLIENT_ERROR if PutCkEnd wasn't called first. */
  395. extern IFFP CloseWGroup(GroupContext *);
  396.             /*  old  */
  397.  
  398. /* Write a whole chunk to a GroupContext. This writes a chunk header, ckSize
  399.  * data bytes, and (if needed) a pad byte. It also updates the GroupContext.
  400.  * CLIENT_ERROR if ckSize == szNotYetKnown. See also PutCkHdr errors. */
  401. extern IFFP PutCk(GroupContext *, ID,   LONG,   BYTE *);
  402.           /*  context,        ckID, ckSize, *data  */
  403.  
  404. /* Write just a chunk header. Follow this will any number of calls to
  405.  * IFFWriteBytes and finish with PutCkEnd.
  406.  * If you don't yet know how big the chunk is, pass in ckSize = szNotYetKnown,
  407.  * then PutCkEnd will set the ckSize for you later.
  408.  * Otherwise, IFFWriteBytes and PutCkEnd will ensure that the specified
  409.  * number of bytes get written.
  410.  * CLIENT_ERROR if the chunk would overflow the GroupContext's bound, if
  411.  * PutCkHdr was previously called without a matching PutCkEnd, if ckSize < 0
  412.  * (except szNotYetKnown), if you're trying to write something other
  413.  * than one FORM, LIST, or CAT in a top level (file level) context, or
  414.  * if ckID <= 0 (these illegal ID values are used for error codes). */
  415. extern IFFP PutCkHdr(GroupContext *, ID,   LONG);
  416.          /*  context,        ckID, ckSize  */
  417.  
  418. /* Write nBytes number of data bytes for the current chunk and update
  419.  * GroupContext.
  420.  * CLIENT_ERROR if this would overflow the GroupContext's limit or the
  421.  * current chunk's ckSize, or if PutCkHdr wasn't called first, or if
  422.  * nBytes < 0. */
  423. extern IFFP IFFWriteBytes(GroupContext *, BYTE *, LONG);
  424.               /*  context,        *data,  nBytes  */
  425.  
  426. /* Complete the current chunk, write a pad byte if needed, and update
  427.  * GroupContext.
  428.  * If current chunk's ckSize = szNotYetKnown, this goes back and sets the
  429.  * ckSize in the file.
  430.  * CLIENT_ERROR if PutCkHdr wasn't called first, or if client hasn't
  431.  * written 'ckSize' number of bytes with IFFWriteBytes. */
  432. extern IFFP PutCkEnd(GroupContext *);
  433.          /*  context  */
  434.  
  435. #endif
  436.  
  437.